package com.cta.view.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cta.entity.PBX;
import com.cta.entity.ServiceProvider;
import com.cta.entity.Trunk;
import com.cta.service.ProfileService;
import com.cta.service.SiteService;
import com.cta.view.json.JsonObject;
import com.cta.view.json.PBXJson;
import com.cta.view.json.ProviderJson;
import com.cta.view.json.TrunkViewJson;

@Controller
public class ServiceProviderController extends BasicController {

	@Autowired
	private SiteService siteService;

	@Autowired
	private ProfileService profileService;

	@Autowired
	private MessageSource messageSource;

	@RequestMapping(value = "/create-service-provider", method = RequestMethod.POST)
	@ResponseBody
	public String createServiceProvider(Locale locale, Long serviceProviderId, String name, String description) {
		try {
			if(serviceProviderId == null) {
				ServiceProvider serviceProvider = profileService.createServiceProvider(name, description);
				return String.format("{'success':true, msg:'%s', name:'%s', id: %s, description: '%s'}","Add Service provider ok",
						serviceProvider.getName(), serviceProvider.getId(), serviceProvider.getDescription());
			} else {
				ServiceProvider serviceProvider = profileService.updateServiceProvider(serviceProviderId, name, description);
				return String.format("{'success':true, msg:'%s', name:'%s', id: %s, description: '%s'}","Update Service provider ok",
						serviceProvider.getName(), serviceProvider.getId(), serviceProvider.getDescription());
			}
		} catch (Exception e) {
			log.error("Error: ", e);
			return String.format("{'success':false, msg:'%s'}","Can not proccess Service Provider");
		}
	}

	
	@RequestMapping(value = "/pbx-service-provider-info", method = RequestMethod.GET)
	@ResponseBody
	public String pbxServiceProviderInfo(Locale locale, Long pbxServiceProviderId) {
		try {
			ServiceProvider serviceProvider  = profileService.findServiceProviderById(pbxServiceProviderId);
			return String.format("{'success':true, msg:'%s', name: '%s', id: %s, description:'%s'}",
					"Get ServiceProvider successful", serviceProvider.getName(), serviceProvider.getId(), serviceProvider.getDescription());
		} catch (Exception e) {
			log.error("Error: ", e);
			return String.format("{'success':false, msg:'%s'}", "Fail to get ServiceProvider");
		}
	}
	
	@RequestMapping(value = "/list-service-provider", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject<ProviderJson> listProvider(Locale locale) {
		try {
			List<ServiceProvider> allProviders = profileService.findAllProviders();
			List<ProviderJson> jsonProviders = new ArrayList<ProviderJson>();
			for (ServiceProvider provider : allProviders) {
				jsonProviders.add(new ProviderJson(provider));
			}
			return new JsonObject<ProviderJson>(true, jsonProviders);
		} catch (Exception e) {
			log.error("Error: ", e);
			return JsonObject.emptyJsonObject();
		}
	}

	@RequestMapping(value = "/create-trunk", method = RequestMethod.POST)
	@ResponseBody
	public String createTrunk(Locale locale, Long trunkId, String name, String description, Long serviceProviderId,
			Long linkTo) {
		try {
			if (trunkId != null && trunkId != 0) {
				profileService.updateTrunk(trunkId, name, description, serviceProviderId, linkTo);
				return String.format("{'success':true, msg:'%s', name:'%s'}", "Update trunk Ok", name);
			} else {
				Trunk trunk = profileService.createTrunk(name, description, serviceProviderId, linkTo);
				return String.format("{'success':true, msg:'%s', name:'%s', obId: %s, thumb: '%s'}", "Create trunk ok",
						trunk.getName(), trunk.getId(), "phone.png");
			}
		} catch (Exception e) {
			log.error("Error: ", e);
			return String.format("{'success':false, msg:'%s'}",
					messageSource.getMessage("profile.create.message.fail", null, locale));
		}
	}

	@RequestMapping(value = "/list-trunk-view", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject<TrunkViewJson> listTrunk(Locale locale, Long serviceProviderId) {
		try {
			List<Trunk> allTrunks = profileService.findTrunkByProvider(serviceProviderId);
			List<TrunkViewJson> jsonTrunks = new ArrayList<TrunkViewJson>(allTrunks.size());
			for (Trunk trunk : allTrunks) {
				jsonTrunks.add(new TrunkViewJson(trunk.getName(), "phone.png", String.valueOf(trunk.getId())));
			}
			return JsonObject.newJsonObject(true, jsonTrunks);
		} catch (Exception e) {
			log.error("Error: ", e);
			return JsonObject.emptyJsonObject();
		}
	}

	@RequestMapping(value = "/trunk-info", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject<TrunkViewJson> trunkInfo(Locale locale, Long trunkId) {
		try {
			Trunk trunk = profileService.findTrunkById(trunkId);
			if (trunk != null) {
				List<PBX> pbxs = siteService.findAllPBX();
				List<PBXJson> pbxView = new ArrayList<PBXJson>(pbxs.size());
				for (PBX p : pbxs) {
					pbxView.add(new PBXJson(String.valueOf(p.getId()), p.getName() + " - "
							+ p.getPbxFactory().getName(), null, null, null, null));
				}
				return JsonObject.newJsonObject(
						true,
						new TrunkViewJson(trunk.getName(), null, String.valueOf(trunk.getId()), trunk.getDescription(),
								String.valueOf(trunk.getServiceProvider().getId()), String.valueOf(trunk.getPbx()
										.getId()), pbxView));
			} else {
				return JsonObject.emptyJsonObject();
			}

		} catch (Exception e) {
			log.error("Error: ", e);
			return JsonObject.emptyJsonObject();
		}
	}

}
